const App = getApp();

// 工具类
import util from '../../../utils/util.js';

// 倒计时插件
import CountDown from '../../../utils/countdown.js';

// 枚举类：秒杀活动商品状态
import ActiveStatusEnum from '../../../utils/enum/sharp/GoodsStatus.js';

Component({

  options: {
    addGlobalClass: true,
  },

  /**
   * 组件的属性列表
   * 用于组件自定义设置
   */
  properties: {
    itemIndex: String,
    itemStyle: Object,
    params: Object,
    data: Object,
  },

  /**
   * 组件私有属性
   */
  data: {
    ActiveStatusEnum, // 秒杀活动商品状态
    countDownList: [], // 倒计时
    sharpData:false
  },

  /**
   * 组件生命周期声明对象
   */
  lifetimes: {

    /**
     * 在组件实例进入页面节点树时执行
     */
    attached() {
      let _this = this;
      console.log('秒杀数据',_this.data.data)
      //_this._initData()
      _this._initCountDown()
    },

  },



  /**
   * 组件的方法列表
   * 更新属性和数据的方法与更新页面数据的方法类似
   */
  methods: {
    /**
     * 获取秒杀数据
     */
    _initData() {
      let t = this
      App.args.request({
        url: App.args.api.miaosha.list,
        success(e) {
          console.log('_initData',e)
          t.setData({
            sharpData: e.alldata
          })
        }
      })
    },
    
    /**
     * 跳转商品详情页
     */
    _onTargetGoods(e) {
      // 生成query参数
      let _this = this,
        query = util.urlEncode({
          active_time_id: _this.data.data.active.active_time_id,
          sharp_goods_id: e.detail.target.dataset.id,
          end_time: _this.data.countDownList[0].date
        });
      // 跳转到商品详情页
      wx.navigateTo({
        url: `/pages/sharp/goods/index?${query}`,
      });
    },

    /**
     * 更多秒杀
     */
    _onTargetSharpIndex(e) {
      // 跳转到秒杀会场首页
      wx.navigateTo({
        url: `/pages/sharp/index/index`,
      });
    },

    /**
     * 初始化倒计时组件
     */
    _initCountDown(data) {
      let t = this
      App.args.request({
        url: App.args.api.miaosha.list,
        success(e) {
          //t.data.sharpData = e.alldata
            let addOneDay = new Date(e.alldata[0].active_date.replace(/-/g,"/")).getTime()+86400000
            addOneDay = new Date(parseInt(addOneDay))
            addOneDay = addOneDay.getFullYear()+"-"+(addOneDay.getMonth()+1)+"-"+addOneDay.getDate()
            t.setData({
              sharpData: e.alldata,
              [`countDownList[0]`]: {
                date: e.alldata[1] ? e.alldata[1].active_date : addOneDay,
              }
            })
            CountDown.onSetTimeList(t, 'countDownList')
        }
      })
    },
    _initCountDownData(data) {
      let _this = this,
        active = _this.data.data.active;
      if (!active) return false;
      // 记录倒计时的时间
      _this.setData({
        [`countDownList[0]`]: {
          date: active.count_down_time,
        }
      });
      // 执行倒计时
      CountDown.onSetTimeList(_this, 'countDownList')
      /*var interval = setInterval(function(){
        _this._getApiData()
        clearInterval(interval)
        setInterval(function() {
          _this._getApiData()
        },3600000)
      },new Date(active.count_down_time.replace(/-/g,"/")).getTime()-Date.parse(new Date())+1000)*/
    },
    // sg添加
    _getApiData() {
      let _this = this
      App._get('sharp.index/index', {}, (result) => {
        if(result.data.tabbar.length != 0) {
          _this.data.data.active = result.data.tabbar[0]
          _this.data.data.goodsList = result.data.goodsList
          _this.setData({
            [`countDownList[0]`]: {
              date: result.data.tabbar[0].count_down_time
            },
            ['countDownList[0].dynamic.min']:59,
            ['countDownList[0].dynamic.sec']:59
          })
          CountDown.onSetTimeList(_this, 'countDownList')
        }
      })

    }
  }
})